// FrogImageResizeDialog.cpp : Implementation of CFrogImageLogoDialog

#include "stdafx.h"
#include "FrogImageLogoDialog.h"
#include "FrogMessageBox.h"
#include <atldlgs.h>


// CFrogImageLogoDialog
CFrogImageLogoDialog::CFrogImageLogoDialog(HICON hIcon):
CFrogProgressReportDialog(hIcon),
m_LogoImagePath(L""),
m_Postion(0),
m_PutInFolder(true),
m_AddBorder(true),
m_Folder(L"Images With Logo")
{
}

LRESULT CFrogImageLogoDialog::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	m_ImageLogoConfig.LoadFromRegistry();

	CFrogProgressReportDialog::OnInitDialog(uMsg, wParam, lParam, bHandled);

	m_ColorBtn1.SubclassWindow(GetDlgItem(IDC_BUTTON_COLOR_BORDER));
	m_ColorBtn1.SetColor(m_ImageLogoConfig.borderColor.ToCOLORREF());

	m_EditFolder.Attach(GetDlgItem(IDC_EDIT_FOLDER));
	m_CheckBoxPutInFolder.Attach(GetDlgItem(IDC_CHECK_PUT_IN_FOLDER));
	m_CheckBoxAddBorder.Attach(GetDlgItem(IDC_CHECK_ADD_BORDER));

	m_RadioLogoFile.Attach(GetDlgItem(IDC_RADIO_FILE));
	m_RadioLogoText.Attach(GetDlgItem(IDC_RADIO_TEXT));

	if (m_ImageLogoConfig.useImage)
	{
		m_RadioLogoFile.SetCheck(TRUE);
	}
	else
	{
		m_RadioLogoText.SetCheck(TRUE);
	}

	m_PutInFolder = m_ImageLogoConfig.putInFolder;
	m_AddBorder =  m_ImageLogoConfig.useBorder;

	m_Postion = m_ImageLogoConfig.postion;

	m_LogoImagePath = m_ImageLogoConfig.logoFile;
	m_Folder = m_ImageLogoConfig.folder;
	m_LogoText = m_ImageLogoConfig.logoText;

	if (!m_PutInFolder)
	{
		m_EditFolder.EnableWindow(FALSE);
	}

	if (!m_AddBorder)
	{
		m_ColorBtn1.EnableWindow(FALSE);
	}

	m_FontName.Attach(GetDlgItem(IDC_STATIC_FONT));

	UpdateFontLabel(m_FontName, m_ImageLogoConfig.logoFont);	

	DoDataExchange(FALSE);

	EnableDisableButtons();

	return 0;
}

LRESULT CFrogImageLogoDialog::OnClickedOk(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	bHandled = TRUE;
	// TODO: Add your control notification handler code here
	DoDataExchange(TRUE);

	m_ImageLogoConfig.borderColor.SetFromCOLORREF(m_ColorBtn1.GetColor());

	m_ImageLogoConfig.postion = m_Postion;

	m_ImageLogoConfig.putInFolder = m_CheckBoxPutInFolder.GetCheck() == TRUE;	
	m_ImageLogoConfig.useBorder = m_CheckBoxAddBorder.GetCheck() == TRUE;		

	wcscpy_s(m_ImageLogoConfig.folder,MAX_PATH, m_Folder.GetBuffer(0)); 
	m_Folder.ReleaseBuffer();

	m_ImageLogoConfig.useImage = m_RadioLogoFile.GetCheck() == TRUE;
	wcscpy_s(m_ImageLogoConfig.logoFile, MAX_PATH, m_LogoImagePath.GetBuffer(0));
	m_LogoImagePath.ReleaseBuffer();

	wcscpy_s(m_ImageLogoConfig.logoText, 1024, m_LogoText.GetBuffer(0));
	m_LogoText.ReleaseBuffer();

	if (m_CheckBoxPutInFolder.GetCheck() == TRUE)
	{
		if (!FilerFrogUtils::IsFileNameValid(wstring(m_Folder)))
		{
			FrogMessageBox(m_hWnd, L"Please Enter a Valid Folder Name", L"FilerFrog - Image Resize", 0);
			return TRUE;
		}
	}
	else
	{
		m_Folder = L"";
	}

	if (m_RadioLogoFile.GetCheck() == TRUE && (wstring(m_LogoImagePath) == L"" || !PathFileExists(wstring(m_LogoImagePath).c_str())))
	{
		FrogMessageBox(m_hWnd, L"Image Logo File Does not Exists", L"FilerFrog - Image Resize", 0);
		return TRUE;
	}

	m_ImageLogoConfig.SaveToRegistry();

	RunAsyncCommandWithProgressBar(false);

	EndDialog(wID);

	return 0;
}

LRESULT CFrogImageLogoDialog::OnCheckPutInFolderBnClicked(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	if (m_CheckBoxPutInFolder.GetCheck())
	{
		m_EditFolder.EnableWindow(TRUE);
	}
	else
	{
		m_EditFolder.EnableWindow(FALSE);
	}

	return 0;
}

LRESULT CFrogImageLogoDialog::OnCheckAddBorder(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	if (m_CheckBoxAddBorder.GetCheck())
	{
		m_ColorBtn1.EnableWindow(TRUE);
	}
	else
	{
		m_ColorBtn1.EnableWindow(FALSE);
	}

	return 0;
}

LRESULT CFrogImageLogoDialog::OnClickedBrowse(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	TCHAR szFilters[] = _T("All Image Formats (*.bmp;*.gif;*.jpg;*.jpeg;*.tif;*.png)\0*.bmp;*.gif;*.jpg;*.jpeg;*.tif;*.png\0Bitmaps (*.bmp)\0*.bmp\0GIFs (*.gif)\0*.gif\0JPEGs (*.jpg)\0*.jpg;*.jpeg\0TIFs (*.tif)\0*.tif\0PNGs (*.png)\0*.png\0\0");

	CFileDialog fileDialog(TRUE, NULL, NULL, OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, szFilters, m_hWnd);

	if (fileDialog.DoModal() != IDCANCEL)
	{
		DoDataExchange(TRUE);
		m_LogoImagePath = fileDialog.m_szFileName;
		DoDataExchange(FALSE);
	}

	return 0;
}

LRESULT CFrogImageLogoDialog::OnButtonFont(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	GetSelectedFont(m_FontName);
	return 0;
}

void CFrogImageLogoDialog::GetSelectedFont(CStatic& fontName)
{
	CFontDialog fontDialog;
	fontDialog.SetLogFont(&m_ImageLogoConfig.logoFont);
	fontDialog.m_cf.rgbColors = m_ImageLogoConfig.fontColor.ToCOLORREF();

	if (fontDialog.DoModal() == IDOK)
	{
		fontDialog.GetCurrentFont(&m_ImageLogoConfig.logoFont);
		UpdateFontLabel(fontName, m_ImageLogoConfig.logoFont);
		m_ImageLogoConfig.fontColor.SetFromCOLORREF(fontDialog.GetColor());
	}
}

LRESULT CFrogImageLogoDialog::OnClickedRadioLogo(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
{
	EnableDisableButtons();
	return 0;
}

void CFrogImageLogoDialog::UpdateFontLabel(CStatic& fontName, const LOGFONTW& font)
{
	int fontSize = - MulDiv(font.lfHeight ,72, GetDeviceCaps(GetDC(), LOGPIXELSY));
	WCHAR buffer[1024];
	swprintf_s(buffer, 1024, L"%s, %dpt", font.lfFaceName, fontSize);
	fontName.SetWindowText(buffer);
}

void CFrogImageLogoDialog::ExecuteCommand()
{
	m_FilesRefactor.LogoOnImages(m_LastResult, m_FilesList, wstring(m_LogoImagePath), wstring(m_LogoText), m_ImageLogoConfig.logoFont, m_ImageLogoConfig.fontColor, m_CheckBoxAddBorder.GetCheck() == TRUE,  m_ImageLogoConfig.borderColor, m_RadioLogoFile.GetCheck() == TRUE, (IMAGE_LOC)m_Postion, wstring(m_Folder));
}

void CFrogImageLogoDialog::EnableDisableButtons()
{
	DoDataExchange(TRUE);

	GetDlgItem(IDC_EDIT_LOGO).EnableWindow(m_RadioLogoFile.GetCheck() == TRUE);
	GetDlgItem(IDC_BUTTON_BROWSE).EnableWindow(m_RadioLogoFile.GetCheck() == TRUE);

	GetDlgItem(IDC_EDIT_LOGO_TEXT).EnableWindow(m_RadioLogoFile.GetCheck() != TRUE);
	GetDlgItem(IDC_BUTTON_FONT).EnableWindow(m_RadioLogoFile.GetCheck() != TRUE);
	GetDlgItem(IDC_STATIC_FONT).EnableWindow(m_RadioLogoFile.GetCheck() != TRUE);
}
